package com.itheima.supermarket.manage.dao;

import com.itheima.supermarket.manage.domain.Fruits;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class FruitDao {

    //获取水果集合
    public ArrayList<Fruits> getList() {
        //创建一个水果集合对象
        ArrayList<Fruits> fruits = new ArrayList<>();

        BufferedReader br = null;
        try {
            //创建水果文本输入流对象
            br = new BufferedReader(new FileReader("Fruit\\txt\\Fruits.txt"));

            //读取库存水果信息，封装对象，添加到集合中
            String line;
            while ((line = br.readLine()) != null) {
                String[] split = line.split("，");
                Fruits fruit = new Fruits(split[0], split[1], split[2], split[3]);
                fruits.add(fruit);
            }

            //对集合进行排序
            Collections.sort(fruits, new Comparator<Fruits>() {
                @Override
                public int compare(Fruits o1, Fruits o2) {
                    int result = o1.getNumber().compareTo(o2.getNumber());
                    return result;
                }
            });

            //对水果集合做非空判断
            if (fruits.size() == 0) {
                fruits = null;
            }
        } catch (IOException e) {
            System.out.println("Dao里的getList()输入流出问题了！");
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                System.out.println("Dao里的getList()关闭流出问题了！");
            }
            return fruits;
        }
    }

    //添加水果
    public void addFruits(Fruits fruit) {

        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("Fruit\\txt\\Fruits.txt", true));
            String s = fruit.getNumber() + "，" + fruit.getName() + "，" + fruit.getPrice() + "，" + fruit.getUnit();
            bw.write(s);
            bw.newLine();
        } catch (IOException e) {
            System.out.println("Dao里的addFruits()输出流出问题了！");
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                System.out.println("Dao里的addFruits()关闭流出问题了！");
            }
        }
    }

    //删除水果
    public void deleteFruits(String number) {
        //先通过集合删除集合中指定删除的水果
        ArrayList<Fruits> fruits = getList();
        int index = getIndex(fruits, number);
        fruits.remove(index);

        //同步水果库方法
        synchro(fruits);
    }

    //修改水果
    public void updateFruits(Fruits fruit, String number) {
        ArrayList<Fruits> fruits = getList();
        int index = getIndex(fruits, number);
        fruits.set(index, fruit);

        synchro(fruits);
    }

    //获取指定集合对象编号的对象索引
    public int getIndex(ArrayList<Fruits> fruits, String number) {
        int index = -1;
        for (int i = 0; i < fruits.size(); i++) {
            if (fruits.get(i).getNumber().equals(number)) {
                index = i;
            }
        }
        return index;
    }

    //同步水果库
    public void synchro(ArrayList<Fruits> fruits) {
        //再通过IO流写入存水果信息的指定文本
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("Fruit\\txt\\Fruits.txt"));
            for (Fruits fruit : fruits) {
                String s = fruit.getNumber() + "，" + fruit.getName() + "，" + fruit.getPrice() + "，" + fruit.getUnit();
                bw.write(s);
                bw.newLine();
            }
        } catch (IOException e) {
            System.out.println("Dao里的synchro()输出流出问题了！");
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                System.out.println("Dao里的synchro()关闭流出问题了！");
            }
        }
    }
}
